package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.entity.realTime.EntityNewUserCashAddByHour;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 实时数据-新增账号付费金额manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class NewUserCashAddByHourManager extends ManagerQueryCacheBase<EntityNewUserCashAddByHour> {
	private static final long serialVersionUID = 1L;
	
	public List<EntityNewUserCashAddByHour> getChannelHourNewUserCashAddByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityNewUserCashAddByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid !=:channelid and serverid=:serverid order by hour desc ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	public List<EntityNewUserCashAddByHour> getHourNewUserCashAddByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityNewUserCashAddByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid order by hour ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	// 根据平台、渠道、区服更新新增账号付费金额数据
	public void updateHourNewUserCashAddByConditions(String updateType, Map<Integer, OverviewDetailData> map, Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer) {
		List<EntityNewUserCashAddByHour> ooLookDate = getHourNewUserCashAddByDate(lookDate, queryPlatform, queryChannel, queryServer);
		List<EntityNewUserCashAddByHour> ooCompareDate = getHourNewUserCashAddByDate(compareDate, queryPlatform, queryChannel, queryServer);
        List<EntityNewUserCashAddByHour> ooLastWeekDate = getHourNewUserCashAddByDate(lastWeekDate, queryPlatform, queryChannel, queryServer);
        if ("1".equals(updateType)) {  // 分时
            updateData(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        } else if ("2".equals(updateType)) {  // 累加
            updateData1(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        }
	}
	
	// 更新结果-分时
	private void updateData(Map<Integer, OverviewDetailData> map, List<EntityNewUserCashAddByHour> ooLookDate, List<EntityNewUserCashAddByHour> ooCompareDate, List<EntityNewUserCashAddByHour> ooLastWeekDate) {
		for (EntityNewUserCashAddByHour o : ooLookDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setNewUserPayLookValue(o.getNew_user_cashadd() + odd.getNewUserPayLookValue());
				odd.setNewUserPayLookValue(UIHelper.getYuan(odd.getNewUserPayLookValue()));
			}
		}
		
		for (EntityNewUserCashAddByHour o : ooCompareDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setNewUserPayCompareValue(o.getNew_user_cashadd() + odd.getNewUserPayCompareValue());
				odd.setNewUserPayCompareValue(UIHelper.getYuan(odd.getNewUserPayCompareValue()));
			}
		}

        for (EntityNewUserCashAddByHour o : ooLastWeekDate) {
            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                odd.setNewUserPayLastWeekValue(o.getNew_user_cashadd() + odd.getNewUserPayLastWeekValue());
                odd.setNewUserPayLastWeekValue(UIHelper.getYuan(odd.getNewUserPayLastWeekValue()));
            }
        }
	}

    // 更新结果-累计
    private void updateData1(Map<Integer, OverviewDetailData> map, List<EntityNewUserCashAddByHour> ooLookDate, List<EntityNewUserCashAddByHour> ooCompareDate, List<EntityNewUserCashAddByHour> ooLastWeekDate) {
    	for (EntityNewUserCashAddByHour o : ooLookDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setNewUserPayLookValue(o.getNew_user_cashadd() + odd.getNewUserPayLookValue());
			}
		}
		
		for (EntityNewUserCashAddByHour o : ooCompareDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setNewUserPayCompareValue(o.getNew_user_cashadd() + odd.getNewUserPayCompareValue());
			}
		}

        for (EntityNewUserCashAddByHour o : ooLastWeekDate) {
            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                odd.setNewUserPayLastWeekValue(o.getNew_user_cashadd() + odd.getNewUserPayLastWeekValue());
            }
        }
        
        // 累计规则：三个日期中，与本日相同的那个的累计规则特殊，需要判断它的数据列表中最大的hour，这个hour之后的数据不累加且计为0
        String todayStr = Time.getDateStr(Time.currentDate());
        OverviewDetailData oneData = map.get(0);
        boolean isLookToday = todayStr.equals(oneData.getLookDateStr());
        int maxLookHour = (ooLookDate == null || ooLookDate.size() == 0) ? 0 : ooLookDate.get(ooLookDate.size() - 1).getHour();
        boolean isCompareToday = todayStr.equals(oneData.getCompareDateStr());
        int maxCompareHour = (ooCompareDate == null || ooCompareDate.size() == 0) ? 0 : ooCompareDate.get(ooCompareDate.size() - 1).getHour();
        boolean isLastWeekToday = todayStr.equals(oneData.getLastWeekDateStr());
        int maxLastWeekHour = (ooLastWeekDate == null || ooLastWeekDate.size() == 0) ? 0 : ooLastWeekDate.get(ooLastWeekDate.size() - 1).getHour();

        int sum1 = 0,sum2 = 0,sum3 = 0;
        //由于传入的map子类是LinkedHashMap且根据hour顺序初始化，故不再寻找最大hour从0开始累计 下同
		for(Map.Entry<Integer, OverviewDetailData> entry : map.entrySet()){
		    int hour = entry.getKey();
            sum1 += entry.getValue().getNewUserPayLookValue();
		    if (!isLookToday || hour <= maxLookHour) {
                entry.getValue().setNewUserPayLookValue(UIHelper.getYuan(sum1));
            }
			sum2 += entry.getValue().getNewUserPayCompareValue();
		    if (!isCompareToday || hour <= maxCompareHour) {
                entry.getValue().setNewUserPayCompareValue(UIHelper.getYuan(sum2));
            }
			sum3 += entry.getValue().getNewUserPayLastWeekValue();
			if (!isLastWeekToday || hour <= maxLastWeekHour) {
                entry.getValue().setNewUserPayLastWeekValue(UIHelper.getYuan(sum3));
            }
		}
    }
}
